package com.sunny;

import java.text.DateFormat;
import java.util.Date;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * Hello world!
 */
public class App {

    public static void main(String[] args) throws Exception {
        if (args.length < 4) {
            System.err.println("input param : host、port、account、password");
            return;
        }

        String host = args[0];
        Integer port = Integer.parseInt(args[1]);
        String account = args[2];
        String password = args[3];

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setPort(port);
        javaMailSender.setUsername(account);
        javaMailSender.setPassword(password);

        String[] to = {account};
        Date sendDate = new Date();

        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        helper.setFrom(new InternetAddress(account, account, "UTF-8"));
        helper.setTo(to);
        helper.setSubject("[邮件服务器验证]");
        helper.setText("这是一封服务器验证邮件！<br/>服务器时间：" + DateFormat.getDateTimeInstance().format(sendDate), true);
        helper.setSentDate(sendDate);

        javaMailSender.send(mailMessage);
        System.out.println("OK");
    }

}
